package com.appcenter.task.service;


import com.appcenter.jz.config.JsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.task.config.RemoteProperties;
import com.appcenter.task.dto.Apps;
import com.appcenter.task.input.LoadAlreadyTaskListPageInput;
import com.appcenter.task.input.SearchAlreadyTaskListInput;
import com.appcenter.task.mapper.AlreadyTasksMapper;
import com.appcenter.task.model.AlreadyTasks;
import com.appcenter.task.output.LoadAlreadyTaskListPageOutput;
import com.appcenter.task.output.SearchAlreadyTaskListOutput;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.appcenter.jz.unit.ObjectUnit.JsonUnit;
import static org.apache.http.util.TextUtils.isBlank;

@EnableConfigurationProperties(RemoteProperties.class)
@Service
public class AlreadyTaskService {
    @Autowired
    AlreadyTasksMapper alreadyTasksMapper;
    @Autowired
    RemoteProperties remoteProperties;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    private HttpAPIService httpAPIService;

    /**
     *  创建人:Coke
     *  备注: 加载已办信息
     *  时间: 2019/4/26 14:47 
     *  参数: 
     */
    public LoadAlreadyTaskListPageOutput LoadAlreadyTaskListPage(LoadAlreadyTaskListPageInput input) throws IOException {
        LoadAlreadyTaskListPageOutput output = new LoadAlreadyTaskListPageOutput();
        String GetAppsUrl = remoteProperties.getAppCenterServiceUrl() + "/app/getapps";
        String responseJson = httpAPIService.JsonSMS(GetAppsUrl, "{}");
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        JsonResult jsonResult = objectMapper.readValue(responseJson, JsonResult.class);
        List<Apps> apps = new ArrayList<Apps>();
        Object OneItems = JsonUnit(jsonResult.getResult(), "items");
        ArrayList<Object> TwoItems = (ArrayList<Object>) JsonUnit(OneItems, "items");
        if (TwoItems != null && TwoItems.size() > 0) {
            for (int i = 0; i < TwoItems.size(); i++) {
                Apps app = new Apps();
                app.setId(JsonUnit(TwoItems.get(i), "id").toString());
                app.setAppCode(JsonUnit(TwoItems.get(i), "appCode").toString());
                app.setAppName(JsonUnit(TwoItems.get(i), "appName").toString());
                apps.add(app);
            }
        }
        output.setApps(apps);
        output.setItems(SearchAlreadyTaskList(input).getItems());
        return output;
    }

    /**
     *  创建人:Coke
     *  备注: 搜索已办信息
     *  时间: 2019/4/26 14:52
     *  参数: 
     */
    public SearchAlreadyTaskListOutput SearchAlreadyTaskList(SearchAlreadyTaskListInput input){
        SearchAlreadyTaskListOutput output = new SearchAlreadyTaskListOutput();
        Example example = new Example(AlreadyTasks.class);
        Example.Criteria criteriaKeyword = example.createCriteria();
        if(!isBlank(input.getKeyword())){
            criteriaKeyword.orLike("title", "%"+input.getKeyword()+"%");
            criteriaKeyword.orLike("content", "%"+input.getKeyword()+"%");
            criteriaKeyword.orLike("businessType", "%"+input.getKeyword()+"%");
        }
        example.and(criteriaKeyword);

        Example.Criteria criteriaUserCode = example.createCriteria();
        if(!isBlank(input.getUserCode())){
            criteriaUserCode.orLike("handleUserId", "%"+input.getUserCode()+"%");
            criteriaUserCode.orLike("handleUserCode", "%"+input.getUserCode()+"%");
            criteriaUserCode.orLike("handleUserName", "%"+input.getUserCode()+"%");
        }
        example.and(criteriaUserCode);
        Example.Criteria criteriaMain = example.createCriteria();
        if(!isBlank(input.getAppCode())){
            criteriaMain.andEqualTo("appCode", input.getAppCode());
        }
        example.and(criteriaMain);

        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "createTime desc");
        List<AlreadyTasks> tasks = alreadyTasksMapper.selectByExample(example);
        PageInfo<AlreadyTasks> page = new PageInfo<>(tasks);
        output.setItems(new PagedResult<AlreadyTasks>(page.getList(), (int) page.getTotal()));
        return output;
    }
}
